<template>
  <el-card>
    <el-form label-width="80px">
      <el-form-item label="广告名称"
                    required>
        <el-input v-model="adData.name"></el-input>
      </el-form-item>
      <el-form-item label="广告位置">
        <el-select v-model="adData.spaceId"
                   placeholder="请选择广告位置"
                   clearable>
          <el-option :label="item.name"
                     :value="item.id"
                     v-for="item in adListData"
                     :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上线/下线">
        <el-radio-group v-model="adData.status">
          <el-radio :label="1">上线</el-radio>
          <el-radio :label="0">下线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开始时间"
                    required>
        <el-date-picker v-model="adData.startTimeFormatString"
                        type="date"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="结束时间"
                    required>
        <el-date-picker v-model="adData.endTimeFormatString"
                        type="date"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="课程封面">
        <!--
            upload 上传文件组件，它支持自动上传，你只需要把上传需要参数配置一下就可以了
            -->
        <!--
            1. 组件需要根据绑定的数据进行图片预览
            2. 组件需要把上传成功的图片地址同步到绑定的数据中
            v-model 的本质还是父子组件通信
              1. 它会给子组件传递一个名字叫 value 的数据（Props）
              2. 默认监听 input 事件，修改绑定的数据（自定义事件）
            -->
        <ad-data-image v-model="adData.img"
                       :limit="5" />
        <el-tag type="info">建议尺寸：230 * 300px，JPG，PNG格式，图片小于150K</el-tag>
      </el-form-item>
      <el-form-item label="广告链接"
                    required>
        <el-input v-model="adData.link"></el-input>
      </el-form-item>
      <el-form-item label="广告备注">
        <el-input type="textarea"
                  v-model="adData.text"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="goBack">取消</el-button>
        <el-button type="primary"
                   @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdById, saveOrUpdate } from '@/services/advert'
import { getAdAllList } from '@/services/advert-space'
import AdDataImage from '@/views/course/components/CourseImage.vue'

export default Vue.extend({
  name: 'AdvertEdit',
  components: {
    AdDataImage
  },
  props: {
    id: {
      type: [String, Number]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      adData: {},
      adListData: []
    }
  },

  created() {
    if (this.isEdit) {
      this.loadAd()
    }
    this.loadAdAllList()
  },

  methods: {
    async loadAd() {
      const { data } = await getAdById(this.id)
      this.adData = data.content
    },
    async loadAdAllList() {
      const { data } = await getAdAllList()
      this.adListData = data.content
    },

    async onSubmit() {
      await saveOrUpdate(this.adData)
      this.$message.success('操作成功')
      this.$emit('success')
    },
    goBack() {
      this.$router.back()
    }
  }
})
</script>

<style lang="scss" scoped></style>
